/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Connect.java
 *
 * Created on 05/04/2011, 15:26:06
 */

package forumGUI;

import client.ForumClientListener;
import client.MessagesListener;
import client.TcpClient;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import messaage.AddedForumMsg;
import messaage.AddedFriendMsg;
import messaage.AddedPostMsg;
import messaage.ConnectedMsg;
import messaage.CreatedThreadMsg;
import messaage.DisconnectMsg;
import messaage.DisconnectedMsg;
import messaage.EditedPostMsg;
import messaage.ErrorMsg;
import messaage.GotFriendsListMsg;
import messaage.GotListOfForumsMsg;
import messaage.GotThreadMsg;
import messaage.GotTopicsOfForumMsg;
import messaage.LoggedInMsg;
import messaage.LoggedOffMsg;
import messaage.RegisteredMsg;
import messaage.RemovedForumMsg;
import messaage.RemovedFriendMsg;
import messaage.RemovedPostMsg;
import messaage.RemovedThreadMsg;
import messaage.RemovedUserMsg;

/**
 * 
 * @author benezrab
 */
public class ConnectScreen extends javax.swing.JFrame implements
		MessagesListener {
	private TcpClient client;

	/** Creates new form Connect */
	public ConnectScreen(TcpClient newClient) {
		initComponents();
		this.client = newClient;
                 this.setBackground();

	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        host = new javax.swing.JLabel();
        port = new javax.swing.JLabel();
        portTextField = new javax.swing.JTextField();
        hostTextField = new javax.swing.JTextField();
        connectButon = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Viner Hand ITC", 0, 48));
        jLabel1.setText("Connect");

        host.setFont(new java.awt.Font("Viner Hand ITC", 0, 24));
        host.setText("host:");

        port.setFont(new java.awt.Font("Viner Hand ITC", 0, 24));
        port.setText("port:");

        hostTextField.setText("localhost");
        hostTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                hostTextFieldActionPerformed(evt);
            }
        });

        connectButon.setFont(new java.awt.Font("Tempus Sans ITC", 1, 18));
        connectButon.setText("Connect!");
        connectButon.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                connectButonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(222, 222, 222)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(host)
                                    .addComponent(port))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(18, 18, 18)
                                        .addComponent(portTextField))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(18, 18, 18)
                                        .addComponent(hostTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE))))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(281, 281, 281)
                        .addComponent(connectButon)))
                .addContainerGap(214, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(66, 66, 66)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(64, 64, 64)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(host)
                    .addComponent(hostTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(port)
                    .addComponent(portTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(57, 57, 57)
                .addComponent(connectButon)
                .addGap(116, 116, 116))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

        private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
            this.setDefaultCloseOperation(this.DO_NOTHING_ON_CLOSE);
            DisconnectMsg msg = new DisconnectMsg(null);  //TODO change the username to send
            this.client.sendMessage(msg);
        }//GEN-LAST:event_formWindowClosed

	private void hostTextFieldActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_hostTextFieldActionPerformed
		// TODO add your handling code here:
	}// GEN-LAST:event_hostTextFieldActionPerformed

	private void connectButonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_connectButonActionPerformed
		String host = this.hostTextField.getText();
		String strPort = this.portTextField.getText();
		if (host.equals("") || strPort.equals("")) {
			JOptionPane.showMessageDialog(this, "Please enter host and port.");
		}

		else {
			int port = Integer.parseInt(strPort);
			 this.client.run(host, port, this);
			// TODO delete afer test
			/* ======================================================
			this.setVisible(false);
			MainScreen ms = new MainScreen(this.client);
			ms.setVisible(true);*/
		}

	}// GEN-LAST:event_connectButonActionPerformed

	/**
	 * @param args
	 *            the command line arguments
	 */

           public void setBackground()
    {
        Container con =this.getContentPane();
        JPanel panelBgImg;
        con.setLayout(null);
        final String photoPath = "src/images/screen.jpg";
        ImageIcon imh = new ImageIcon(photoPath);
//        setSize(imh.getIconWidth(), imh.getIconHeight());

        panelBgImg = new JPanel()
        {
            public void paintComponent(Graphics g)
            {
                Image img = new ImageIcon(photoPath).getImage();

                Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
                setPreferredSize(size);
                setMinimumSize(size);
                setMaximumSize(size);
                setSize(size);
                setLayout(null);
                g.drawImage(img, 0, 0, null);
            }
        };

        con.add(panelBgImg);
        panelBgImg.setBounds(0, 0, imh.getIconWidth(), imh.getIconHeight());

    }
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				// new Connect().setVisible(true);
			}
		});
	}
// localhost
	@Override
	public void connected(ConnectedMsg msg) {
		MainScreen ms = new MainScreen(this.client);

                ForumClientListener tempListener = (ForumClientListener) this.client
				.getListener();
		tempListener.getConnector().addListener(ms);
		ms.setVisible(true);
		this.setVisible(false);

	}

	@Override
	public void disconnected(DisconnectedMsg msg) {
		  this.dispose();
	}

	@Override
	public void errorMessage(ErrorMsg msg) {
		if (this.isVisible() == true){
			String body = msg.getBody();
			JOptionPane.showMessageDialog(this, body);
		}
	}

	@Override
	public void registered(RegisteredMsg msg) {
		if (this.isVisible() == true)
			throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public void loggedIn(LoggedInMsg msg) {
		if (this.isVisible() == true)
			throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public void loggedOff(LoggedOffMsg msg) {
		if (this.isVisible() == true)
			throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public void gotListOfForums(GotListOfForumsMsg msg) {
		if (this.isVisible() == true)
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public void gotTopicsOfForum(GotTopicsOfForumMsg msg) {
		if (this.isVisible() == true)
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public void gotThread(GotThreadMsg msg) {
		if (this.isVisible() == true)
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public void addedPost(AddedPostMsg msg) {
		if (this.isVisible() == true)
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public void createdThread(CreatedThreadMsg msg) {
		if (this.isVisible() == true)
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public void addedForum(AddedForumMsg msg) {
		if (this.isVisible() == true)
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public void removedForum(RemovedForumMsg msg) {
		if (this.isVisible() == true)
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public void editedPost(EditedPostMsg msg) {
		if (this.isVisible() == true)
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public void removedUser(RemovedUserMsg msg) {
		if (this.isVisible() == true)
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public void removedThread(RemovedThreadMsg msg) {
		if (this.isVisible() == true)
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public void removedPost(RemovedPostMsg msg) {
		if (this.isVisible() == true)
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public void addedFriend(AddedFriendMsg msg) {
		if (this.isVisible() == true)
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public void removedFriend(RemovedFriendMsg msg) {
		if (this.isVisible() == true)
		throw new UnsupportedOperationException("Not supported yet.");
	}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton connectButon;
    private javax.swing.JLabel host;
    private javax.swing.JTextField hostTextField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel port;
    private javax.swing.JTextField portTextField;
    // End of variables declaration//GEN-END:variables

	@Override
	public void gotFriendsList(GotFriendsListMsg msg) {
	}

}
